-- appearances
-- statusline style
-- lvim.builtin.lualine.style = "default" -- "default" or "none" or "lvim"
-- lvim.builtin.lualine.sections.lualine_c = { "diff" }
-- end appearances
-- general settings
vim.opt.cmdheight = 1 -- more space in the neovim command line for displaying messages
vim.opt.fileencoding = "utf-8" -- the encoding written to a file
vim.opt.timeoutlen = 300 -- time to wait for a mapped sequence to complete (in milliseconds)
vim.opt.relativenumber = true -- set relative numbered lines
-- vim.opt.backup = false -- creates a backup file
-- vim.opt.clipboard = "unnamedplus" -- allows neovim to access the system clipboard
-- vim.opt.colorcolumn = "99999" -- fixes indentline for now
-- vim.opt.completeopt = { "menuone", "noselect" }
-- vim.opt.conceallevel = 0 -- so that `` is visible in markdown files
-- vim.opt.foldmethod = "manual" -- folding set to "expr" for treesitter based folding
-- vim.opt.foldexpr = "" -- set to "nvim_treesitter#foldexpr()" for treesitter based folding
-- vim.opt.guifont = "monospace:h17" -- the font used in graphical neovim applications
-- vim.opt.hidden = true -- required to keep multiple buffers and open multiple buffers
-- vim.opt.hlsearch = true -- highlight all matches on previous search pattern
-- vim.opt.ignorecase = true -- ignore case in search patterns
-- vim.opt.mouse = "a" -- allow the mouse to be used in neovim
-- vim.opt.pumheight = 10 -- pop up menu height
-- vim.opt.showmode = false -- we don't need to see things like -- INSERT -- anymore
-- vim.opt.showtabline = 2 -- always show tabs
-- vim.opt.smartcase = true -- smart case
-- vim.opt.smartindent = true -- make indenting smarter again
-- vim.opt.splitbelow = true -- force all horizontal splits to go below current window
-- vim.opt.splitright = true -- force all vertical splits to go to the right of current window
-- vim.opt.swapfile = false -- creates a swapfile
-- vim.opt.termguicolors = true -- set term gui colors (most terminals support this)
-- vim.opt.title = true -- set the title of window to the value of the titlestring
-- vim.opt.titlestring = "%<%F%=%l/%L - nvim" -- what the title of the window will be set to
-- vim.opt.undodir = vim.fn.stdpath "cache" .. "/undo"
-- vim.opt.undofile = true -- enable persistent undo
-- vim.opt.updatetime = 300 -- faster completion
-- vim.opt.writebackup = false -- if a file is being edited by another program (or was written to file while editing with another program) it is not allowed to be edited
-- vim.opt.expandtab = true -- convert tabs to spaces
-- vim.opt.shiftwidth = 2 -- the number of spaces inserted for each indentation
-- vim.opt.tabstop = 2 -- insert 2 spaces for a tab
-- vim.opt.cursorline = true -- highlight the current line
-- vim.opt.number = true -- set numbered lines
-- vim.opt.numberwidth = 2 -- set number column width to 2 {default 4}
-- vim.opt.signcolumn = "yes" -- always show the sign column otherwise it would shift the text each time
-- vim.opt.wrap = false -- display lines as one long line
-- vim.opt.spell = false
-- vim.opt.spelllang = "en"
-- vim.opt.scrolloff = 8 -- is one of my fav
-- vim.opt.sidescrolloff = 8
lvim.log.level = "error"
lvim.format_on_save = true
lvim.colorscheme = "onedarker"
lvim.transparent_window = true
-- end general settings

-- keymappings
-- lvim style [view all the defaults by pressing <leader>Lk]
-- unmap a default keymapping
-- lvim.keys.normal_mode["<C-Up>"] = false
lvim.leader = "space"
lvim.keys.normal_mode["<C-s>"] = ":w<cr>"
lvim.keys.normal_mode["<C-q>"] = ":q<cr>"

-- keymappings neovim style
-- vim.api.nvim_set_keymap('n', '<Leader><Space>', ':set hlsearch!<CR>', { noremap = true, silent = true })
-- Vim equivalent
-- :nnoremap <silent> <Leader><Space> :set hlsearch<CR>
-- X closes a buffer
-- lvim.keys.normal_mode["<S-x>"] = ":BufferClose<CR>"
-- use the default vim behavior for H and L
-- lvim.keys.normal_mode["<S-l>"] = false
-- lvim.keys.normal_mode["<S-h>"] = false
-- vim.opt.scrolloff = 0 -- Required so L moves to the last line
-- lvim.keys.normal_mode = {
-- Page down/up
--   ["[d"] = "<PageUp>",
--   ["]d"] = "<PageDown>",

-- Navigate buffers
--   ["<Tab>"] = ":bnext<CR>",
--   ["<S-Tab>"] = ":bprevious<CR>",
-- }

-- keymappings vim style
-- Just take your vim keybindings and wrap them in vim.cmd
-- vim.cmd("nnoremap W :w<CR>")

-- Multiline Statements
-- vim.cmd([[
--    map <Leader>bb :!bundle install<cr>
--    map <Leader>gdm :Git diff master<cr>
--    imap jj <esc>
-- ]])

-- vim.cmd("nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>")
-- end keymappings

-- lsp config
-- ---@usage disable automatic installation of servers
lvim.lsp.automatic_servers_installation = false

local formatters = require("lvim.lsp.null-ls.formatters")
formatters.setup({
    {
        -- each formatter accepts a list of options identical to https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md#Configuration
        command = "prettier",
        ---@usage arguments to pass to the formatter
        -- these cannot contain whitespaces, options such as `--line-width 80` become either `{'--line-width', '80'}` or `{'--line-width=80'}`
        extra_args = {"--print-with", "100"},
        ---@usage specify which filetypes to enable. By default a providers will attach to all the filetypes it supports.
        filetypes = {"typescript", "typescriptreact"}
    }, {command = "lua-format", filetypes = {"lua"}},

    {command = "yapf", filetypes = {"python"}}
})

-- -- set additional linters
local linters = require("lvim.lsp.null-ls.linters")
linters.setup {
    {command = "flake8", filetypes = {"python"}}, {
        -- each linter accepts a list of options identical to https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/doc/BUILTINS.md#Configuration
        command = "shellcheck",
        ---@usage arguments to pass to the formatter
        -- these cannot contain whitespaces, options such as `--line-width 80` become either `{'--line-width', '80'}` or `{'--line-width=80'}`
        extra_args = {"--severity", "warning"}
    }
    --, {
    --    command = "codespell",
    --    ---@usage specify which filetypes to enable. By default a providers will attach to all the filetypes it supports.
    --    filetypes = {"c", "cpp", "javascript", "python"}
    -- }
}

-- ---@usage Select which servers should be configured manually. Requires `:LvimCacheRest` to take effect.
-- See the full default list `:lua print(vim.inspect(lvim.lsp.override))`
-- vim.list_extend(lvim.lsp.override, { "pyright" })

-- ---@usage setup a server -- see: https://www.lunarvim.org/languages/#overriding-the-default-configuration
-- local opts = {} -- check the lspconfig documentation for a list of all possible options
-- require("lvim.lsp.manager").setup("pylsp", opts)

-- -- you can set a custom on_attach function that will be used for all the language servers
-- -- See <https://github.com/neovim/nvim-lspconfig#keybindings-and-completion>
-- lvim.lsp.on_attach_callback = function(client, bufnr)
--   local function buf_set_option(...)
--     vim.api.nvim_buf_set_option(bufnr, ...)
--   end
--   --Enable completion triggered by <c-x><c-o>
--   buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
-- end

-- end lsp configs

-- builtin plugins
lvim.builtin.dashboard.active = true
lvim.builtin.notify.active = true
lvim.builtin.terminal.active = true
lvim.builtin.nvimtree.setup.view.side = "left"
lvim.builtin.nvimtree.show_icons.git = 0
lvim.builtin.lualine.active = true
lvim.builtin.dap.active = true
lvim.builtin.bufferline.active = true -- this is actually using romgrk/barbar.nvim

-- if you don't want all the parsers change this to a table of the ones you want
lvim.builtin.treesitter.ensure_installed = {
    -- "bash",
    "c", "cpp", "go", "javascript", "json", "lua", "python", "typescript",
    "yaml", "rust"
    -- "css",
    -- "rust",
    -- "java",
}

lvim.builtin.treesitter.ignore_install = {"haskell"}
lvim.builtin.treesitter.highlight.enabled = true

--- Use which-key to add extra bindings with the leader-key prefix
-- lvim.builtin.which_key.mappings["P"] = {
--     "<cmd>Telescope projects<CR>", "Projects"
-- }
-- lvim.builtin.which_key.mappings["t"] = {
--     name = "+Trouble",
--     r = {"<cmd>Trouble lsp_references<cr>", "References"},
--     f = {"<cmd>Trouble lsp_definitions<cr>", "Definitions"},
--     d = {"<cmd>Trouble lsp_document_diagnostics<cr>", "Diagnostics"},
--     q = {"<cmd>Trouble quickfix<cr>", "QuickFix"},
--     l = {"<cmd>Trouble loclist<cr>", "LocationList"},
--     w = {"<cmd>Trouble lsp_workspace_diagnostics<cr>", "Diagnostics"}
-- }

-- Change Telescope navigation to use j and k for navigation and n and p for history in both input and normal mode.
-- we use protected-mode (pcall) just in case the plugin wasn't loaded yet.
-- end builtin plugins
-- local _, actions = pcall(require, "telescope.actions")
-- lvim.builtin.telescope.defaults.mappings = {
--     -- for input mode
--     i = {
--         ["<C-j>"] = actions.move_selection_next,
--         ["<C-k>"] = actions.move_selection_previous,
--         ["<C-n>"] = actions.cycle_history_next,
--         ["<C-p>"] = actions.cycle_history_prev
--     },
--     -- for normal mode
--     n = {
--         ["<C-j>"] = actions.move_selection_next,
--         ["<C-k>"] = actions.move_selection_previous
--     }
-- }

-- custom plugins
lvim.plugins = {
    {"dstein64/vim-startuptime", cmd = "StartupTime"},

    {"lunarvim/colorschemes"}, {"nathom/filetype.nvim"}, {
        "karb94/neoscroll.nvim",
        event = "WinScrolled",
        config = function()
            require('neoscroll').setup({
                -- All these keys will be mapped to their corresponding default scrolling animation
                mappings = {
                    '<C-u>', '<C-d>', '<C-b>', '<C-f>', '<C-y>', '<C-e>', 'zt',
                    'zz', 'zb'
                },
                hide_cursor = true, -- Hide cursor while scrolling
                stop_eof = true, -- Stop at <EOF> when scrolling downwards
                use_local_scrolloff = false, -- Use the local scope of scrolloff instead of the global scope
                respect_scrolloff = false, -- Stop scrolling when the cursor reaches the scrolloff margin of the file
                cursor_scrolls_alone = true, -- The cursor will keep on scrolling even if the window cannot scroll further
                easing_function = nil, -- Default easing function
                pre_hook = nil, -- Function to run before the scrolling animation starts
                post_hook = nil -- Function to run after the scrolling animation ends
            })
        end
    }
}
vim.g.did_load_filetypes = 1
-- end custom plugins

-- Autocommands (https://neovim.io/doc/user/autocmd.html)
-- lvim.autocommands.custom_groups = {
--   { "BufWinEnter", "*.lua", "setlocal ts=8 sw=8" },
--- autodcommands
lvim.autocommands.custom_groups = {
    -- On entering a lua file, set the tab spacing and shift width to 8
    {"BufWinEnter", "*.lua", "setlocal ts=4 sw=4"},

    -- On entering insert mode in any file, scroll the window so the cursor line is centered
    {"InsertEnter", "*", ":normal zz"}
}
-- end autocommands

